<?php

    class M_account extends CI_Model
    {
        
        public function lay_nhan_vien()
        {
            $this->db->where(array('ROLE_ID'=>1));
            $this->db->or_where(array('ROLE_ID'=>3));
            $result= $this->db->get('ACCOUNT');
            if($result->num_rows()>0)
                return $result->result();
            else
                return false;
        }
        
        
        
        
        
        
        
        
        
        
        
        //lấy hết account
        public function get_all_account()
        {
            $result= $this->db->get('ACCOUNT');
            if($result->num_rows()>0)
                return $result->result();
            else
                return false;
        }
        function update_wallet($id,$ngaycapnhat,$walletmoi)
    {
            $data['ACCOUNT_ID']=$id;
            $data['MODIFIED_DATE']=$ngaycapnhat;
            $data['WALLET']=$walletmoi;
            return $this->db->insert('wallet_log',$data);
    }
        public function get_all_account_new()
        {
             $query=$this->db->query('SELECT account.*, temp.WALLET
                                        FROM account
                                        LEFT JOIN (
                                            select a.ACCOUNT_ID, a.MODIFIED_DATE, a.WALLET
                                            	from wallet_log a
                                            join (select max(WALLET_ID)as id, ACCOUNT_ID, max(modified_date) as Ngay_max from wallet_log group by ACCOUNT_ID) b
                                            	on a.WALLET_ID = b.id
                                            	and b.Ngay_max = a.MODIFIED_DATE
                                            	
                                            ) temp
                                        ON account.ACCOUNT_ID = temp.ACCOUNT_ID
                                        ');
            if($query->num_rows()>0)
                return $query->result();
            else return false;
        }
        //đếm
        public function total_account()
        {
            return $this->db->count_all('ACCOUNT');
        }
        //lấy account phân trang
        public function get_acount_per_page($limit,$start)
        {
            $this->db->limit($limit);
            $this->db->offset($start);
            $result= $this->db->get('ACCOUNT');
            if($result->num_rows()>0)
                return $result->result();
            else
                return false;
        }
        //lấy account theo account ID
         public function get_account_by_id($id)
        {
            $query=$this->db->query("SELECT account.*, temp.WALLET
                                        FROM account
                                        LEFT JOIN (
                                            select a.ACCOUNT_ID, a.MODIFIED_DATE, a.WALLET
                                            	from wallet_log a
                                            join (select max(WALLET_ID)as id, ACCOUNT_ID, max(modified_date) as Ngay_max from wallet_log group by ACCOUNT_ID) b
                                            	on a.WALLET_ID = b.id
                                            	and b.Ngay_max = a.MODIFIED_DATE
                                            ) temp
                                        ON account.ACCOUNT_ID = temp.ACCOUNT_ID
                                        WHERE account.ACCOUNT_ID = $id;
                                        ");
            if($query->num_rows()>0)
                return $query->row();
            else return false;
        }

        //Lấy account theo account username
        public function get_account_by_username($username)
        {
           $query=$this->db->query("SELECT account.*, temp.WALLET
                                        FROM account
                                        LEFT JOIN (
                                            select a.ACCOUNT_ID, a.MODIFIED_DATE, a.WALLET
                                            	from wallet_log a
                                            join (select max(WALLET_ID)as id, ACCOUNT_ID, max(modified_date) as Ngay_max from wallet_log group by ACCOUNT_ID) b
                                            	on a.WALLET_ID = b.id
                                            	and b.Ngay_max = a.MODIFIED_DATE
                                            ) temp
                                        ON account.ACCOUNT_ID = temp.ACCOUNT_ID
                                        WHERE account.USERNAME = '$username';
                                        ");
            if($query->num_rows()>0)
                return $query->row();
            else return false;
        }

        //lấy account theo facebook
         public function get_account_by_fb($fb)
        {
            $this->db->where(array('FACEBOOK'=>$fb));
            $result= $this->db->get('ACCOUNT');
            if($result->num_rows()>0)
                return $result->row();
            else
                return false;
        }
          //lấy account theo Steam profile
         public function get_account_by_steam($steam)
        {
            $this->db->where(array('STEAM_PROFILE'=>$steam));
            $result= $this->db->get('ACCOUNT');
            if($result->num_rows()>0)
                return $result->row();
            else
                return false;
        }
        //xóa 1 account (need more information)
        public function delete_account($id)
        {
            $this->db->where(array('ACCOUNT_ID'=>$id));
            return $this->db->delete('ACCOUNT');
        }
        public function get_role_by_id($id)
        {
            $this->db->where(array('ACCOUNT_ID'=>$id));
            $this->db->select('ROLE_ID');
            $result= $this->db->get('ACCOUNT');
            if($result->num_rows()>0)
                return $result->row();
            else
                return false;
        }
        
        public function add_new_account($ROLE_ID,$USERNAME,$PASSWORD,$EMAIL,$FULL_NAME,$FACEBOOK,$STEAM_PROFILE,$STEAMOFFER_URL,$ADDRESS,$PHONE_NUMBER,$AVATAR)
        {
            $data['ROLE_ID']=$ROLE_ID;
            $data['USERNAME']=$USERNAME;
            $data['PASSWORD']=md5(md5($PASSWORD));
            $data['EMAIL']=$EMAIL;
            $data['FULL_NAME']=$FULL_NAME;
            $data['FACEBOOK']=$FACEBOOK;
            $data['STEAM_PROFILE']=$STEAM_PROFILE;
            $data['STEAMOFFER_URL']=$STEAMOFFER_URL;
            $data['ADDRESS']=$ADDRESS;
            $data['PHONE_NUMBER']=$PHONE_NUMBER;
            $data['AVATAR']=$AVATAR;
            return $this->db->insert('ACCOUNT',$data);
        }
        public function update_account($ACCOUNT_ID,$ROLE_ID,$USERNAME,$PASSWORD,$EMAIL,$FULL_NAME,$FACEBOOK,$STEAM_PROFILE,$STEAMOFFER_URL,$ADDRESS,$PHONE_NUMBER,$AVATAR)
        {
            $data['ROLE_ID']=$ROLE_ID;
            $data['USERNAME']=$USERNAME;
            $data['PASSWORD']=md5(md5($PASSWORD));
            $data['EMAIL']=$EMAIL;
            $data['FULL_NAME']=$FULL_NAME;
            $data['FACEBOOK']=$FACEBOOK;
            $data['STEAM_PROFILE']=$STEAM_PROFILE;
            $data['STEAMOFFER_URL']=$STEAMOFFER_URL;
            $data['ADDRESS']=$ADDRESS;
            $data['PHONE_NUMBER']=$PHONE_NUMBER;
            $data['AVATAR']=$AVATAR;
            return $this->db->update('ACCOUNT',$data,array('ACCOUNT_ID'=>$ACCOUNT_ID));
        }
        
        // information of account
        
        
        //role
         public function get_all_role()
        {
            $result= $this->db->get('ROLE');
            if($result->num_rows()>0)
                return $result->result();
            else
                return false;
        }
        public function add_new_role($ROLE_NAME,$ROLE_DESCRIPTION)
        {
            $data['ROLE_NAME']=$ROLE_NAME;
            $data['ROLE_DESCRIPTION']=$ROLE_DESCRIPTION;
            return $this->db->insert('ROLE',$data);
        }
        public function update_role($ROLE_ID,$ROLE_NAME,$ROLE_DESCRIPTION)
        {
            $data['ROLE_NAME']=$ROLE_NAME;
            $data['ROLE_DESCRIPTION']=$ROLE_DESCRIPTION;
            return $this->db->update('ROLE',$data,array('ROLE_ID'=>$ROLE_ID));
        }
         public function delete_role($id)
        {
            $this->db->where(array('ROLE_ID'=>$id));
            return $this->db->delete('ROLE');
        }

        public function get_info_role_by_id($id)
        {
            $this->db->where('ROLE_ID', $id);
            $result= $this->db->get('role');
            if($result->num_rows()>0)
                return $result->row();
            else
                return false;
        }

        //bank account
        public function get_all_bank_account()
        {
            $result= $this->db->get('BANK_ACCOUNT');
            if($result->num_rows()>0)
                return $result->result();
            else
                return false;
        }
         public function deletebankaccount($id)
        {
            $this->db->where(array('BANK_ID'=>$id));
            return $this->db->delete('BANK_ACCOUNT');
        }
         public function addbank($accountid, $bankname, $banknumber)
        {
            $data['ACCOUNT_ID']=$accountid;
            $data['BANK_NAME']=$bankname;
            $data['BANK_NUMBER']=$banknumber;
            return $this->db->insert('bank_account',$data);
        }

        public function loadbanks($accountid)
        {
            $this->db->where('ACCOUNT_ID', $accountid);
            $result = $this->db->get('bank_account');
            if($result->num_rows()>0)
                return $result->result();
            else
                return false;
        }
        
        // Login
        public function checklogin($username, $password)
        {
            $this->db->where('USERNAME', $username);
            $this->db->where('PASSWORD', $password);
            $result = $this->db->get('account');
            if($result->num_rows()>0)
                return true;
            else
                return false;
        }

        // Check username
        public function check_username($username)
        {
            $this->db->where('USERNAME', $username);
            $result = $this->db->get('account');
            if($result->num_rows()>0)
                return true;
            else
                return false;
        }

        public function changepassword($accountid, $password)
        {
            $data['PASSWORD']=$password;
            return $this->db->update('ACCOUNT',$data,array('ACCOUNT_ID'=>$accountid));
        }

        public function updateaccount1($ACCOUNT_ID,$EMAIL,$FULL_NAME,$FACEBOOK,$STEAM_PROFILE,$STEAMOFFER_URL,$ADDRESS,$PHONE_NUMBER)
        {
            $data['EMAIL']=$EMAIL;
            $data['FULL_NAME']=$FULL_NAME;
            $data['FACEBOOK']=$FACEBOOK;
            $data['STEAM_PROFILE']=$STEAM_PROFILE;
            $data['STEAMOFFER_URL']=$STEAMOFFER_URL;
            $data['ADDRESS']=$ADDRESS;
            $data['PHONE_NUMBER']=$PHONE_NUMBER;
            return $this->db->update('ACCOUNT',$data,array('ACCOUNT_ID'=>$ACCOUNT_ID));
        }
    }

?>